/*===================================================================
infutor.do
	*Author(s):	David Phillips
	*Purpose:	Creates person-level outcomes from Infutor address histories
				The input file inf_scc_fuzzymatch_final is received
				from SCC based on matching code we send them.
				(infutor_matcher_scc)
===================================================================*/


*** Merging to intake to get start date
	clear
	use "$rawdir/inf_scc_fuzzymatch_final HP 2.2.2022.dta"
	*this requires both names individually at least 0.5 and one of them better than that
	*keep if similscore_name > 0.5
	keep if (SSNmatch == 1 & similscore_name >= 0.5) | (similscore_name > 0.5)
	*
	gen HMISID = LEOid
	
	merge m:1 HMISID using "$datadir/assmt.dta", keepusing(HMISID startdate)
	drop if _merge == 1
	gen infmatch = (_merge == 3)
	drop _merge
	
	
***	 Computing outcome measures

	forvalues x = 1(1)10 {
		gen date_addstart_`x' = mofd(mdy(Effective_Date_`x' - 100*floor(Effective_Date_`x'/100), 1 ,floor(Effective_Date_`x'/100)))
		format date_addstart_`x' %td
		
		gen date_addend_`x' = mofd(mdy(Last_Verification_Address_`x' - 100*floor(Last_Verification_Address_`x'/100), 1 ,floor(Last_Verification_Address_`x'/100)))
		format date_addend_`x' %td
	}

	gen month_assess = mofd(startdate)


	forvalues x = 1(1)24 {
		gen any_add_`x' = 0
		forvalues y = 1(1)10 {
			replace any_add_`x' = 1 if date_addstart_`y' <= month_assess + `x' & date_addend_`y' >= month_assess
		}
		replace any_add_`x' = . if mofd(mdy(8,1,2021)) - month_assess < `x'
	}


	forvalues x = 1(1)24 {
		gen new_add_`x' = 0
		forvalues y = 1(1)10 {
			replace new_add_`x' = 1 if date_addstart_`y' >= month_assess & date_addstart_`y' <= month_assess + `x'
			replace new_add_`x' = 1 if date_addend_`y' >= month_assess & date_addend_`y' <= month_assess + `x'		
		}
		replace new_add_`x' = . if mofd(mdy(8,1,2021)) - month_assess < `x'
	}

	forvalues x = 1(1)24 {
		gen new_addpre_`x' = 0
		forvalues y = 1(1)10 {
			replace new_addpre_`x' = 1 if date_addstart_`y' < month_assess & date_addstart_`y' >= month_assess - `x'
			replace new_addpre_`x' = 1 if date_addend_`y' < month_assess & date_addend_`y' >= month_assess - `x'
		}
	}

	gen infmatch_pre = 0
	forvalues y = 1(1)10{
		replace infmatch_pre = 1 if infmatch == 1 & date_addstart_`y' < month_assess 
	}


	forvalues x = 1(1)24 {
		gen new_add_notscc_`x' = 0
		forvalues y = 1(1)10 {
			replace new_add_notscc_`x' = 1 if (State_`y' ~= "CA" | Fips_County_`y' ~= "085" ) & date_addstart_`y' >= month_assess & date_addstart_`y' <= month_assess + `x'
			replace new_add_notscc_`x' = 1 if (State_`y' ~= "CA" | Fips_County_`y' ~= "085" ) &  date_addend_`y' >= month_assess & date_addend_`y' <= month_assess + `x'		
		}
		replace new_add_notscc_`x' = . if mofd(mdy(8,1,2021)) - month_assess < `x'
	}

	forvalues x = 1(1)24 {
		gen new_addpre_notscc_`x' = 0
		forvalues y = 1(1)10 {
			replace new_addpre_notscc_`x' = 1 if (State_`y' ~= "CA" | Fips_County_`y' ~= "085" ) &  date_addstart_`y' < month_assess & date_addstart_`y' >= month_assess - `x'
			replace new_addpre_notscc_`x' = 1 if (State_`y' ~= "CA" | Fips_County_`y' ~= "085" ) &  date_addend_`y' < month_assess & date_addend_`y' >= month_assess - `x'
		}
	}
	
***moving from multiple Infutor records per person to one summary observation

	collapse (max) infmatch* new_add_* new_addpre_* any_add_*, by(HMISID)
	save "$datadir/infutor.dta", replace

	



